package com.microsoft.socialphoto.ui;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.odata4j.consumer.ODataConsumer;
import org.odata4j.core.OEntity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.microsoft.socialphoto.Config;
import com.microsoft.socialphoto.R;
import com.microsoft.socialphoto.SocialActivity;
import com.microsoft.socialphoto.adapter.ImageLoader;
import com.microsoft.socialphoto.odata.OData;
import com.microsoft.socialphoto.utils.PromptDialog;
import com.microsoft.socialphoto.utils.Utils;

public class PhotoDetail extends SocialActivity implements OnClickListener {

	private ImageView ivPhoto;
	private long photoID;
	private Button tvLocation;
	private TextView tvTitle;
	private Button btnShare;
	private Button btnComment;
	private Button btnDislike;
	private Button btnLike;
	private Short numLike;
	private Short numDisLike;
	private Button btnShowComment;
	private Double mLat;
	private Double mLong;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_photo_detail);

		Bundle extras = getIntent().getExtras();

		if (null != extras && extras.containsKey("photo_id")) {
			photoID = extras.getLong("photo_id");
		}

		initControls();
		initVariables();
	}

	private void initControls() {
		ivPhoto = (ImageView) findViewById(R.id.photo);
		tvTitle = (TextView) findViewById(R.id.title);
		tvLocation = (Button) findViewById(R.id.location);
		btnLike = (Button) findViewById(R.id.btn_like);
		btnDislike = (Button) findViewById(R.id.btn_dislike);
		btnComment = (Button) findViewById(R.id.btn_comment);
		btnShare = (Button) findViewById(R.id.btn_share);
		btnShowComment = (Button) findViewById(R.id.btn_show_comment);
	}

	private void initVariables() {
		ImageLoader imageLoader = new ImageLoader(this);
		ODataConsumer data = OData.getInstance();
		OEntity photoData = data.getEntities("Photos(" + photoID + ")")
				.execute().first();

		// set title
		String titleName = photoData.getProperty("OriginalFileName",
				String.class).getValue();
		tvTitle.setText(titleName);

		// set long
		mLong = photoData.getProperty("Longitude", Double.class).getValue();
		mLong = null != mLong ? mLong : 0;
		mLat = photoData.getProperty("Latitude", Double.class).getValue();
		mLat = null != mLat ? mLat : 0;

		if (mLong != 0 || mLat != 0) {
			tvLocation.setVisibility(View.VISIBLE);
		}

		// show like
		numLike = photoData.getProperty("Likes", Short.class).getValue();
		btnLike.setText(String.format(getString(R.string.like), numLike));

		// show dislike
		numDisLike = photoData.getProperty("Dislikes", Short.class).getValue();
		btnDislike.setText(String.format(getString(R.string.dislike),
				numDisLike));

		// show image
		imageLoader.DisplayImage(String.format(Config.FULL_IMAGE_SRC, photoID),
				this, ivPhoto);

		btnComment.setOnClickListener(this);
		btnShowComment.setOnClickListener(this);
		btnLike.setOnClickListener(this);
		btnDislike.setOnClickListener(this);
		tvLocation.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		ODataConsumer data = OData.getInstance();

		switch (v.getId()) {
		case R.id.btn_like:
			try {
				data.getEntities("like?id=" + photoID).execute().count();
			} catch (Exception e) {
			}
			btnLike.setText(String.format(getString(R.string.like), ++numLike));
			btnLike.setEnabled(false);
			break;
		case R.id.btn_dislike:
			try {
				data.getEntities("dislike?id=" + photoID).execute().count();
			} catch (Exception e) {
			}
			btnDislike.setText(String.format(getString(R.string.dislike),
					++numDisLike));
			btnDislike.setEnabled(false);
			break;
		case R.id.btn_comment:
			PromptDialog promptDialog = new PromptDialog(this,
					R.string.post_comment, R.string.post_comment_content) {

				@Override
				public boolean onOkClicked(String input) {

					ArrayList<NameValuePair> postData = new ArrayList<NameValuePair>();
					postData.add(new BasicNameValuePair("userId", 1 + ""));
					postData.add(new BasicNameValuePair("photoId", photoID + ""));
					postData.add(new BasicNameValuePair("comment", input));

					HttpResponse reponseData = Utils.sendPostData(
							Config.COMMENT_URL, postData);

					String contentData = "";
					try {
						contentData = Utils
								.getContentFromInputStream(reponseData
										.getEntity().getContent());
					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}

					if (contentData.contains("Comment is posted")) {
						Toast.makeText(PhotoDetail.this,
								R.string.post_comment_success,
								Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(PhotoDetail.this,
								R.string.post_comment_false, Toast.LENGTH_LONG)
								.show();
					}

					return false;
				}
			};

			promptDialog.show();
			break;
		case R.id.btn_show_comment:
			Intent intent = new Intent(this, ShowComment.class);
			intent.putExtra("photo_id", photoID);

			startActivity(intent);

			break;
		case R.id.location:
			Intent i = new Intent(android.content.Intent.ACTION_VIEW,
					Uri.parse("geo:" + mLat + "," + mLong + "?z=16"));
			startActivity(i);
			break;
		}
	}
}
